package org.finesys.system.controller;


import java.util.List;

import org.finesys.common.core.constants.RestApiConfig;
import org.finesys.common.core.constants.ValidationGroup;
import org.finesys.common.core.module.R;
import org.finesys.common.core.util.ValidationUtil;
import org.finesys.common.excel.annotaion.RequestExcel;
import org.finesys.common.excel.annotaion.ResponseExcel;
import org.finesys.common.log.core.annotation.LogAnnotation;
import org.finesys.common.log.core.enums.OperateType;
import org.finesys.system.api.dto.DeptDTO;
import org.finesys.system.api.entity.SysDept;
import org.finesys.system.api.vo.DeptExcelVO;
import org.finesys.system.constants.ControllerRestApiConfig;
import org.finesys.system.service.SysDeptService;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;


@Tag(name = "部门管理")
@RestController
@RequestMapping(ControllerRestApiConfig.DEPT)
@RequiredArgsConstructor
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class SysDeptController {
    private final SysDeptService sysDeptService;

    /**
     * 返回树形菜单集合
     *
     * @param deptName 部门名称
     * @return 树形菜单
     */
    @GetMapping(value = RestApiConfig.TREE)
    public R<?> getTree(String deptName) {
        return R.success(sysDeptService.selectTree(deptName));
    }

    /**
     * 获取部门树
     */
    @Operation(summary = "获取部门树")
    @GetMapping(value = RestApiConfig.PAGE)
    public R<List<DeptDTO>> getDeptTree() {
        return R.success(sysDeptService.getDeptTree());
    }

    /**
     * 获取用户部门树
     */
    @Operation(summary = "获取用户部门树")
    @GetMapping(value = ControllerRestApiConfig.USER + RestApiConfig.PAGE)
    public R<List<DeptDTO>> getUserDeptTree() {
        return R.success(sysDeptService.getUserDeptTree());
    }


    /**
     * 添加
     */
    @Operation(summary = "添加")
    @PostMapping(value = RestApiConfig.ADD)
    public R<Void> add(@RequestBody DeptDTO deptDTO) {
        ValidationUtil.validateParam(deptDTO, ValidationGroup.add.class);
        return R.judge(sysDeptService.addDept(deptDTO));
    }

    /**
     * 修改
     */
    @Operation(summary = "修改")
    @PutMapping(value = RestApiConfig.UPDATE)
    public R<Void> update(@RequestBody DeptDTO deptDTO) {
        ValidationUtil.validateParam(deptDTO, ValidationGroup.update.class);
        return R.judge(sysDeptService.updateById(deptDTO));
    }

    /**
     * 删除
     */
    @Operation(summary = "删除")
    @DeleteMapping(value = RestApiConfig.DELETE + "/{id}")
    public R<Void> delete(@PathVariable Long id) {
        return R.judge(sysDeptService.deleteById(id));
    }

    /**
     * 获取单条
     */
    @Operation(summary = "获取单条")
    @GetMapping(value = RestApiConfig.GET + "/{id}")
    public R<SysDept> findById(@PathVariable Long id) {
        return R.success(sysDeptService.getById(id));
    }

    /**
     * 获取全部
     */
    @Operation(summary = "获取全部")
    @GetMapping(value = RestApiConfig.ALL)
    public R<List<SysDept>> findAll() {
        List<SysDept> list = sysDeptService.findAll();
        return R.success(list);
    }

    /**
     * 获取当前用户部门(包含父级)
     */
    @Operation(summary = "获取当前用户部门(包含父级)")
    @GetMapping("getDeptNameByUserId")
    public R<String> getDeptNameByUserId() {
        return R.success(sysDeptService.getDeptNameByUserId());
    }


    /**
     * 同步部门
     *
     * @return R
     */
    @Operation(summary = "同步部门")
    @LogAnnotation(title = "同步部门", operateType = OperateType.CLEAN)
    @PutMapping(RestApiConfig.SYNC)
    public R<?> sync() {
        return sysDeptService.sync();
    }


    /**
     * 导入
     */
    @PostMapping(RestApiConfig.IMPORT_EXCEL)
    @PreAuthorize("@pms.hasPermission('sys_dept_import')")
    public R<?> importExcel(@RequestExcel List<DeptExcelVO> excelVOList, BindingResult bindingResult) {
        return sysDeptService.importExcel(excelVOList, bindingResult);
    }

    /**
     * 导出
     */
    @ResponseExcel
    @GetMapping(RestApiConfig.EXPORT_EXCEL)
    @PreAuthorize("@pms.hasPermission('sys_dept_export')")
    public List<DeptExcelVO> exportExcel() {
        return sysDeptService.listDept();
    }
}
